package com.landerer.gui.emgmt;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;

import com.landerer.dto.EmployeeDTO;
import com.landerer.service.EmployeeServiceRemoteBusiness;

@Named
@RequestScoped
public class EmployeeBean implements Serializable {

	private static final long serialVersionUID = -6572072698474894569L;
	
	@EJB
	private EmployeeServiceRemoteBusiness service;

	private List<EmployeeDTO> employees = new ArrayList<EmployeeDTO>();

	public List<EmployeeDTO> getEmployees() {
		return employees;
	}

	public void setEmployees(List<EmployeeDTO> employees) {
		this.employees = employees;
	}
	
	public String findAllEmployees() {
		List<EmployeeDTO> employees = service.findAllEmployees();
		setEmployees(employees);
		return "emgmtOverview";
	}
	
	public String deleteAllEmployees() {
		this.service.deleteAllEmployees();
		return null;
	}
	
	public void updateEmployee(RowEditEvent event) {
		Object editedObject = event.getObject();
		if(editedObject instanceof EmployeeDTO) {
			EmployeeDTO employeeDTO = (EmployeeDTO) editedObject;
			this.service.updateEmployee(employeeDTO);
		}
	}
}
